from django.contrib.auth.models import Group

from users.models import User
from rest_framework import serializers

#1,admin序列化器
class AdminMangerSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = "__all__"

    # 1. 重写create方法，设置管理员，密码加密
    def create(self,validated_data):
        # 1,创建用户对象
        user = super().create(validated_data)

        # 2.设置管理员
        user.is_superuser = True
        user.is_staff = True

        # 3.密码加密入库

        user.set_password(validated_data['password'])
        user.save()

        # 4.返回
        return user



    # 2.重写update方法
    def update(self,instance,validated_data):
        # 1.修改其他信息
        super().update(instance,validated_data)

        # 2.修改密码
        instance.set_password(validated_data["password"])

        instance.save()

        # 3.返回
        return instance


class PermissionGruopSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = ('id','name')
